package com.njupt.content.service.impl;

import com.fasterxml.jackson.databind.node.BigIntegerNode;
import com.njupt.common.pojo.EasyUIDataGridResult;
import com.njupt.common.pojo.EasyUITreeNode;
import com.njupt.common.pojo.TaotaoResult;
import com.njupt.content.service.ItemContentCategoryService;
import com.njupt.mapper.TbContentCategoryMapper;
import com.njupt.pojo.TbContentCategory;
import com.njupt.pojo.TbContentCategoryExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.CRC32;

/**
 * @Author: XJL
 * @Description:
 * @Date: Create in 15:44 2018/5/28
 * @Modified By:
 **/
@Service
public class ItemContentCategoryServiceImpl implements ItemContentCategoryService
{
    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;

    @Override
    public List<EasyUITreeNode> getItemCategoryList(long parentId) {

        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(example);
        List<EasyUITreeNode> easyUITreeNodeList = new ArrayList<>();
        if (list!=null&&list.size()>0){
            for (TbContentCategory c :
                    list) {
                EasyUITreeNode node = new EasyUITreeNode();
                node.setText(c.getName());
                node.setState(c.getIsParent()?"closed":"open");
                node.setId(c.getId());
                easyUITreeNodeList.add(node);
            }


        }

        return easyUITreeNodeList;
    }

    @Override
    public TaotaoResult addContentCategory(long parentId, String name) {

        TbContentCategory tbContentCategory = new TbContentCategory();
        tbContentCategory.setName(name);
        tbContentCategory.setStatus(1);
        tbContentCategory.setParentId(parentId);
        tbContentCategory.setIsParent(false);
        tbContentCategory.setUpdated(new Date());
        tbContentCategory.setSortOrder(1);
        tbContentCategory.setCreated(new Date());
        long id = tbContentCategoryMapper.insert(tbContentCategory);
        TbContentCategory contentCategory = tbContentCategoryMapper.selectByPrimaryKey(parentId);
        if (!contentCategory.getIsParent()){
            contentCategory.setIsParent(true);
            tbContentCategoryMapper.updateByPrimaryKey(contentCategory);
        }

        return TaotaoResult.ok(tbContentCategory);
    }

    @Override
    public TaotaoResult updateContentCategory(long id, String name) {
        TbContentCategory recoder = new TbContentCategory();
        recoder.setUpdated(new Date());
        recoder.setId(id);
        recoder.setName(name);
        tbContentCategoryMapper.updateByPrimaryKeySelective(recoder);


        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult deleteContentCategory(long id) {

      TbContentCategory contentCategory = tbContentCategoryMapper.selectByPrimaryKey(id);
      if(!contentCategory.getIsParent()){
          tbContentCategoryMapper.deleteByPrimaryKey(id);

          return TaotaoResult.ok();
      }else {
          return deleteNode(id);
      }


    }

    /**
     * 递归删除节点,根据parentId(id)查出该节点下的所有子节点，遍历该阶段的子节点，如果为叶子节点则根据parentId直接删除
     * 否则该节点依然为父节点，则递归
     * @param id
     * @return
     */
    public TaotaoResult deleteNode(long id){
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id);
        List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(example);
        for (TbContentCategory c:list
             ) {
            long parentId = c.getParentId();
            if (!c.getIsParent()){
                //如果该节点为叶子节点直接删除
                TbContentCategoryExample example1 = new TbContentCategoryExample();
                TbContentCategoryExample.Criteria criteria1 = example1.createCriteria();
                criteria1.andParentIdEqualTo(parentId);
                tbContentCategoryMapper.deleteByExample(example1);
            }else {
                deleteNode(c.getId());
            }
        }
        return TaotaoResult.ok();
    }
}
